<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Nameplate extends BaseModel
{
    protected $connection = 'api';
    protected $nameplateTable = 'nameplate';
    protected $nameplateUserTable = 'nameplate_user';
    protected $userTable = 'user';
    protected $room_table = 'room';//房间表
    protected $union_room = 'union_room';//公会房间
    protected $union = 'union';//公会表

    //铭牌新增
    public function nameplateAdd($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/nameplateAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //铭牌编辑
    public function nameplateEdit($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/nameplateEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //铭牌用户下发记录新增
    public function nameplateUserAdd($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/nameplateUserAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //铭牌用户下发记录编辑
    public function nameplateUserEdit($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/nameplateUserEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //铭牌用户下发记录删除
    public function nameplateUserDel($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/nameplateUserDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //铭牌列表
    public function nameplateList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $nameplate_id = $request->input('nameplate_id', 0);
        $nameplate_name = $request->input('nameplate_name', '');

        $query = DB::connection($this->connection)->table($this->nameplateTable)->where('is_delete', 0)->orderBy('id', 'desc');
        if(!empty($nameplate_id)) {
            $query = $query->where('id', $nameplate_id);
        }
        if(!empty($nameplate_name)) {
            $query = $query->where('nameplate_name', 'like', '%'.$nameplate_name.'%');
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);

        if(!empty($result['data'])) {
            foreach($result['data'] as $key=>$val) {
                $val['nameplate_img'] = build_image($val['nameplate_img']);
                $val['nameplate_resource'] = build_image($val['nameplate_resource']);

                $result['data'][$key] = $val;
            }
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //铭牌下发列表
    public function nameplateUserList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $user_id = $request->input('user_id', 0);
        $time_st = $request->input('time_st', '');
        $time_et = $request->input('time_et', '');
        $nameplate_id = $request->input('nameplate_id', 0);
        $nameplate_name = $request->input('nameplate_name', '');

        $query = DB::connection($this->connection)->table($this->nameplateUserTable.' as a')
            ->join($this->nameplateTable.' as b', "a.nameplate_id", "=", "b.id")
            ->orderBy('a.id', 'desc')
            ->select(['a.*','b.nameplate_name','b.nameplate_img','b.nameplate_resource','b.nameplate_desc'])
            ->where('a.is_delete', 0);
        if(!empty($user_id)) {
            $query = $query->where('a.user_id', $user_id);
        }
        if(!empty($time_st)) {
            $time_st = date("Y-m-d H:i:s", strtotime($time_st));
            $query = $query->where('a.time_st', '>=', $time_st);
        }
        if(!empty($time_et)) {
            $time_et = date("Y-m-d H:i:s", strtotime($time_et));
            $query = $query->where('a.time_st', '<=', $time_et);
        }
        if(!empty($nameplate_id)) {
            $query = $query->where('a.nameplate_id', $nameplate_id);
        }
        if(!empty($nameplate_name)) {
            $query = $query->where('b.nameplate_name', 'like', '%'.$nameplate_name.'%');
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {
            $userIds = array_column($result['data'], 'user_id');
            $userList = DB::connection($this->connection)->table($this->userTable)->whereIn('user_id', $userIds)->get();
            $userList = array_column(objToArray($userList), null, 'user_id');

            $adminIds = array_column($result['data'], 'admin_id');
            $adminList = DB::table('users')->whereIn('id', $adminIds)->select(['id','name'])->get();
            $adminList = array_column(objToArray($adminList), null, 'id');

            foreach($result['data'] as $key=>$val) {
                $val['nickname'] = empty($userList[$val['user_id']]['nickname']) ? '' : $userList[$val['user_id']]['nickname'];
                $val['uuid'] = empty($userList[$val['user_id']]['uuid']) ? '' : $userList[$val['user_id']]['uuid'];
                $val['admin_name'] = empty($adminList[$val['admin_id']]['name']) ? '' : $adminList[$val['admin_id']]['name'];
                $val['nameplate_img'] = build_image($val['nameplate_img']);
                $val['nameplate_resource'] = build_image($val['nameplate_resource']);

                $result['data'][$key] = $val;
            }
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

}
